package com.openapi.protocol.decode.subPackageHandle.handle;

import com.openapi.config.JT808ServerConfig;
import com.openapi.data.JT808PackageData;
import com.openapi.data.JT808PushData;
import com.openapi.protocol.decode.subPackageHandle.SubPackageHandle;
import com.openapi.protocol.decode.subPackageHandle.hj212Stream.FileBitStreamCache;
import com.openapi.protocol.decode.subPackageHandle.hj212Stream.FileInfoCache;
import com.openapi.service.KafkaService;
import exsun.jt808.data.config.jt808.Jt808ReceiveMsgTopic;
import exsun.jt808.data.upload_data.Jt808EntityBase;
import exsun.jt808.data.upload_data.jsatl12.FileInfo;
import exsun.jt808.data.upload_data.jsatl12.FileSaveFinished;
import exsun.jt808.data.upload_data.jt808.FileBitStreamLoad;
import exsun.jt808.data.util.parse.DecoderUtils;
import exsun.jt808.data.util.parse.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 文件数据上传完成处理
 */
@Component
public class FileStreamHandle {
    protected static KafkaService kafkaService;

    @Autowired
    private void setKafkaService(KafkaService kafkaService) {
        FileStreamHandle.kafkaService = kafkaService;
    }

    protected static JT808ServerConfig jt808ServerConfig;

    @Autowired
    private void setJT808ServerConfig(JT808ServerConfig jt808ServerConfig) {
        FileStreamHandle.jt808ServerConfig = jt808ServerConfig;
    }



    public static Jt808EntityBase decode(JT808PackageData packageData) {
        byte[] data = packageData.getBodyBuffers();
        FileBitStreamLoad obj = new FileBitStreamLoad();
        obj.setFileName(DecoderUtils.parseStringFromBytes(data,4,50).trim());
        obj.setDataOffset(DecoderUtils.parseIntFromBytes(data, 54, 4));
        obj.setData(DecoderUtils.byteCopy(data, 62,data.length-62));
        return obj;
    }

    public static List<JT808PushData> response(FileBitStreamLoad data, String dvo)
    {
        FileBitStreamCache.AddFileBitStream(data);
        if(!FileBitStreamCache.bReciveAll(data.getFileName())) {
            return null;
        }
        byte[] stream = FileBitStreamCache.getWholeStream(data.getFileName());
        if(stream == null||stream.length == 0)return null;
        String path = FileBitStreamCache.uploadfileFtp(dvo, data.getFileName(),stream);

        FileSaveFinished fileSaveFinished = new FileSaveFinished();
        FileInfo fileInfo = FileInfoCache.getFileInfo(data.getFileName());
        fileSaveFinished.setDvo(fileInfo.getPhoneNum());
        fileSaveFinished.setFne(fileInfo.getFileName());
        fileSaveFinished.setFte(fileInfo.getFileType());
        fileSaveFinished.setFse(fileInfo.getFileSize());
        fileSaveFinished.setAcd(fileInfo.getAlarmCode());
        fileSaveFinished.setAdt(getDateTimeFromAlarmCode(fileInfo.getAlarmCode()));
        fileSaveFinished.setCnl(fileInfo.getChannel());
        fileSaveFinished.setAtp(fileInfo.getAlarmType());
        fileSaveFinished.setSno(fileInfo.getSeriNo());
        fileSaveFinished.setPth(path);
        kafkaService.push(fileInfo.getPhoneNum(), Jt808ReceiveMsgTopic.fileSaveFinished, fileSaveFinished);
        FileBitStreamCache.removeStream(data.getFileName());
        FileInfoCache.removeFileInfo(data.getFileName());
        return null;
    }

    private static Date getDateTimeFromAlarmCode(String alarmCode)
    {
        String timestr = alarmCode.substring(alarmCode.length() - 12, alarmCode.length());
        return TimeUtil.getDateByFormat(timestr, TimeUtil.dateFormatyyMMddHHmmss);
    }
}
